﻿using System;
using System.Collections.Generic;
using System.Text;
using Abp.Dependency;
using Abp.Events.Bus.Entities;
using Abp.Events.Bus.Handlers;
using Abp.RemoteEventBus;
using Abp.Runtime.Caching;
using FwProject.BrandInfo.Dto;
using FwProject.Comm;
using FwProject.Core.AppVersion;
using FwProject.Keyword.Dto;
using FwProject.ShoppingMall.BrandInfo;
using FwProject.ShoppingMall.Keyword.Dto;

namespace FwProject.EventHandle
{
    public class KeywordInfoHandle : IEventHandler<EntityChangedEventData<KeywordInfo>>, IEventHandler<EntityDeletingEventData<KeywordInfo>>, ITransientDependency
    {
        public readonly ICacheManager _catchmanager;
        public IRemoteEventBus remoteEventBus { get; set; }//缓存对象
        public KeywordInfoHandle(ICacheManager catchmanager)
        {
            _catchmanager = catchmanager;
            remoteEventBus = NullRemoteEventBus.Instance;
        }
        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="eventData">Event data.</param>
        public void HandleEvent(EntityChangedEventData<KeywordInfo> eventData)
        {
            ICache cache = _catchmanager.GetCache(nameof(KeywordInfo));
            cache.Remove("list");

            if (eventData.Entity.isActive == true)
            {
                var catcheventDate = new RemoteEventData(RemoteTypeHelp.TYPESEARCH)
                {
                    Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(KeywordAppDto) }
                };
                remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, catcheventDate);
            }
            else {
                var catcheventDate = new RemoteEventData(RemoteTypeHelp.TYPEDELETESEARCH)
                {
                    Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(KeywordAppDto) }
                };
                remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, catcheventDate);

            }
         
        }

        public void HandleEvent(EntityDeletingEventData<KeywordInfo> eventData)
        {

            var catcheventDate = new RemoteEventData(RemoteTypeHelp.TYPEDELETESEARCH)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(KeywordAppDto) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, catcheventDate);
        }

    }
}
